﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Text;
using RecordPlaybackFramework;


namespace Orchard.Test.Library
{
    public class LiveWriterHelper : UIHelperBase
    {
        private Process appProcess;
        private string postTitle;
        private string postContent;
        //regsvr32 RPFMSAAPlayback.dll           //Notes:Register these 2 dll before run this class  
        //regasm /codebase RPFUiaManaged.dll

        public LiveWriterHelper(TestLibrary settings) : base(settings)
        {
           
        }

        public void InitRPF()
        {            
              if (RPF.TryInitPlayBack())
                    RPF.SetAccTechnology(RPF.MSAA);
              else
                {
                    try
                    {
                        TestLibrary.WriteErrorInfoToLog(TestLibrary.LogCategory.RPF_LiveWriter, "", "TryInitPlayBackTheFirstTime", "Try to Init RPF PlayBack failed.");
                        Thread.Sleep(1000);
                        try
                        {
                            RPF.FinishPlayBack();
                        }
                        catch (Exception)
                        { }
                        Thread.Sleep(1000);
                        RPF.InitPlayBack();
                        RPF.SetAccTechnology(RPF.MSAA);
                    }
                    catch (System.Runtime.InteropServices.COMException)
                    {
                        RPF.FinishPlayBack(); 
                        RPF.InitPlayBack();
                        RPF.SetAccTechnology(RPF.MSAA);
                    }
                    catch (Exception ex)
                    {
                        TestLibrary.WriteErrorInfoToLog(TestLibrary.LogCategory.RPF_LiveWriter, "", "InitPlayBackAgainAfterTryFinishRpfInInitRpfFunction", ex.ToString());
                    }
                }
            
            //catch (Exception ex)
            //{
            //    TestLibrary.WriteErrorInfoToLog(TestLibrary.LogCategory.RPF_LiveWriter, "", "InitPlayBack", ex.ToString());
            //    Thread.Sleep(1000);
            //    try { RPF.FinishPlayBack(); }
            //    catch (Exception exx)
            //    {
            //        TestLibrary.WriteErrorInfoToLog(TestLibrary.LogCategory.RPF_LiveWriter, "", "TryFinishRpfInInitRpfFunction", exx.ToString());
            //        Thread.Sleep(1000);
            //    }
            //}
             // Process s = Process.GetCurrentProcess(); int id=RPF.MW_PROCESS_IDLE;               
             
        }

        public void FinishRPF()
        {
            try
            {
                RPF.FinishPlayBack();
            }
            catch (Exception ex)
            {
                TestLibrary.WriteErrorInfoToLog(TestLibrary.LogCategory.RPF_LiveWriter, "", "FinishRpf", ex.ToString());
            }
        }

        public Process LaunchLiveWriter()
        {            
            string path = @"C:\Program Files\Windows Live\Writer\WindowsLiveWriter.exe";
            ProcessStartInfo proInfo = new ProcessStartInfo();
            proInfo.FileName = path;
            proInfo.WorkingDirectory = @"d:\Workspace";
            //proInfo.Verb = @"runas";
            appProcess = Process.Start(proInfo);
            return appProcess;
        }

        public string LaunchAndCheckLiveWriterDefault()
        {
            return LaunchAndCheckLiveWriterByBlogAccount("http://localhost/default-test-blog", "Default Test Blog", TestLibrary.Consts.AdminUserName, TestLibrary.Consts.CommonPassword);
        }

        public string LaunchAndCheckLiveWriterByBlogAccount(string blogHomePageUrl,string blogName,string userName,string password)
        {
            try
            {
                LaunchLiveWriter();
                CloseRecoverUnsavedChangedWindow(); 
            }
            catch (Exception)
            {
                return "Can not run Live Writer,please make sure your 'WindowsLiveWriter.exe' is locate on C:\\Program Files\\Windows Live\\Writer\\";
            }           
            if (CheckSettingOfLiveWriter(blogName) != "OK")
            {
               //return UpdateBlogAccount(blogHomePageUrl,blogName,userName, password);
                try
                {
                    ClickBlogAccountDropdown();
                }
                catch(Exception ex)
                { return "Live Writer not ready,please check and do some settings. " + ex.ToString(); }
                if (CheckBlogExist(blogName))
                {
                    SelectBlog(blogName);
                    return "OK";
                }
                else
                {                   
                    return AddBlogAccount(blogHomePageUrl, blogName, userName, password);
                }               
            }
            else return "OK";
        }

        public string CheckSettingOfLiveWriter(string blogName)
        {
            Thread.Sleep(6000);
            string WarningInfo;
            //string blogName = blogHomePageUrl.Substring(blogHomePageUrl.LastIndexOf('/')+1);
            if (!RPF.IsAvailable(15000, string.Format(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Publish' && Role = 'tool bar';[VisibleOnly]Name = '{0}' && Role = 'drop down button'", blogName)))
            {
                WarningInfo = string.Format(">o_o< Hi,Please check blog account!({0})", blogName);
                return WarningInfo;
            }
            else
            {
                OpenMenuOptionPanel();
                EnterPublishingSettings(false, true, false, false, false);
                ClickOKAtOptionsPage();
                return "OK";
            }
        }

        public void CloseLiveWriter()
        {
            Thread.Sleep(9000);
            CloseLiveWriterWindows_byCloseButton();
            try
            {                
                appProcess.Kill();                                
            }
            catch (Exception)
            {
            }
        }

        public void CloseLiveWriterWindows_byCloseButton()
        {
            Thread.Sleep(12000);
            CloseLiveWriterWindowAndClickCancelSaveChangesIfPopup();
        }

        public void CloseWindow_NewworkConnectionError()
        {
            CloseErrorWindow_NetworkConnect();
        }

        public void ConnectToBlogByInvalidAccount()
        {
            ClickBlogAccountDropdown();
            TryAddBlogByInvalidAccount(TestLibrary.Consts.DefaulBlogUrl_Writer, "Default Test Blog2", "admin0", "123456");
        }

        public string AddBlogAccountFromToolBar(string blogHomePageUrl, string blogName, string userName, string password)
        {
            ClickBlogAccountDropdown();
            return AddBlogAccount(blogHomePageUrl,blogName,userName,password);
        }

        public string AddBlogAccount(string blogHomePageUrl,string blogName,string userName,string password)
        {
            Thread.Sleep(3000);
            OpenAddBlogAccountDialogFromToolBar();
            Thread.Sleep(2000);
            SelectOtherType_AddBlog();
            ClickNext();
            EnterBlogAccountInfo(blogHomePageUrl, userName, password);
            CheckRemeberPassword(true);            
            ClickNext();
            Thread.Sleep(6000);
            CheckXmlRpcModuleStatusOfWeb();            
            DownLoadThemes(true);
            if (!string.IsNullOrEmpty(blogName))
                EnterBlogName(blogName);  
            SwitchToBlog(true);
            ClickFinish();
            return "OK";
        }     

        public string UpdateBlogAccount(string blogHomePageUrl,string blogName,string userName,string password)
        {
            Thread.Sleep(3000);
            OpenUpdateAccountForm();            
            EnterBlogAccountInfo(blogHomePageUrl,userName,password);
            CheckRemeberPassword(true);
            ClickNext();
            Thread.Sleep(6000);
            CheckXmlRpcModuleStatusOfWeb();            
            DownLoadThemes(true);
            if(!string.IsNullOrEmpty(blogName))
            EnterBlogName(blogName);
            Thread.Sleep(3000);
            ClickFinish();
            Thread.Sleep(6000);
            ClickOKAtUpdateAccount();
            return "OK";
        }

        public string PublishBlogPost()
        {
           return PublishBlogPost(true);
        }

        public string PublishBlogPost(bool publish)
        {
            Thread.Sleep(3000);
            postTitle = "Live Writer Post";
            EnterPostTitle(postTitle);
            postContent = string.Format("This is a post comes from Live Writer at {0}", DateTime.Now);
            EnterPostContent(postContent);
            if (publish)
            {
                Publish();
                SigninWhenNeed();
                return postTitle + "&" + postContent;
            }
            else return "";
        }

        public void ClickPublish()
        {
            Publish();
        }

        public string PublishBlogPostLater()
        {           
            Thread.Sleep(3000);
            postTitle = "Live Writer Post Later";
            EnterPostTitle(postTitle);
            postContent = string.Format("This is a published_later post comes from Live Writer at {0}", DateTime.Now);
            EnterPostContent(postContent);          
            Thread.Sleep(1000);
            SetPostDate_Add1Minutes(postTitle);
            Publish();
            SigninWhenNeed();
            return postTitle + "&" + postContent;
        }

        public string SaveDraft()
        {
            Thread.Sleep(3000);
            postTitle = "Live Writer Post Draft";
            EnterPostTitle(postTitle);
            postContent = string.Format("This is a post draft saved at {0}", DateTime.Now);
            EnterPostContent(postContent);
            Save();
            return postTitle;
        }

        public string PublishPostWithSpecifySlug()
        {
            Thread.Sleep(3000);
            postTitle = "Live Writer Post With Specify Slug";
            EnterPostTitle(postTitle);
            postContent = string.Format("This is a post with specify slug created at {0}", DateTime.Now);
            EnterPostContent(postContent);
            string postSlug="LiveWriter-PostWithSlug";
            SpecifySlug(postSlug);
            Publish();
            SigninWhenNeed();
            return postTitle + "&" + postSlug;
        }

        public string PublishPostWithTags()
        {
            Thread.Sleep(1000);
            postTitle = "Live Writer Post With Tag";
            EnterPostTitle(postTitle);
            postContent = string.Format("This is a post with tag created at {0}", DateTime.Now);
            EnterPostContent(postContent);
            string postTag = string.Format("Live Writer,Post Tag {0}",DateTime.Now);
            EnterPostTag(postTag);
            Publish();            
            return postTitle + "&" + postTag;
        }

        public void EditAndPostDraftRecentPost(string origTitle,string postTitle)
        {
            EditAndPostDraftRecentPost(origTitle, postTitle, "",true);
        }

        public void EditAndPostDraftRecentPost(string origTitle, string postTitle, string newtags)
        {
            EditAndPostDraftRecentPost(origTitle, postTitle, newtags, true);
        }

        public void EditAndPostDraftRecentPost(string origTitle, string postTitle,string newtags,bool postToBlog)
        {
            OpenPostLinkDefautTestBlog(origTitle);
            if(!string.IsNullOrEmpty(postTitle))  EditPostTitle(origTitle, postTitle);
            EnterPostContent(string.Format("(This is a post edited and postDraft by Writer at {0})", DateTime.Now));
            if(!string.IsNullOrEmpty(newtags)) EditPostTag(newtags);
            if(postToBlog) PostDraftToBlog();
        }

        public void EditAndPublishRecentPost(string origTitle, string postTitle)
        {
            OpenPostLinkDefautTestBlog(origTitle);
            EditPostTitle(origTitle, postTitle);
            EnterPostContent(string.Format("This is a post edited and published by Writer at {0}", DateTime.Now));
            Publish();
        }

        public void DeleteRecentPost(string postTitle)
        {
            DeleteRecentPost(postTitle, true);
        }

        public void DeleteRecentPost(string postTitle,bool delete)
        {
            OpenRecentPostByTitleFromDefautTestBlog(postTitle);
            if (delete)
            {
                ClickDelete();
                ConfirmDelete(true);
                CloseOpenedWindow();
            }            
        }

        public void Delete()
        {
            ClickDelete();
            try
            {
                ConfirmDelete(true);
            }
            catch (Exception)
            { }
        }

        public string PublishPostWithOnLinePicture()
        {
            Thread.Sleep(1000);
            postTitle = "Live Writer Post With Online Picture";
            EnterPostTitle(postTitle);
            postContent = string.Format("This is a post with online picture created at {0}", DateTime.Now);
            EnterPostContent(postContent);
            string pictureUrl = "http://www.orchardproject.net/gallery/Themes/OrchardGallery/Content/Images/branchesTop.png";
            InsertPictureIntoPost_FromWeb(pictureUrl);
            Publish();            
            return postTitle + "&" + pictureUrl;
        }

        public string PublishPostWithLocalPicture(string imageName)
        {
            Thread.Sleep(1000);
            postTitle = "Live Writer Post With Local Picture";
            EnterPostTitle(postTitle);
            postContent = string.Format("This is a post with local picture created at {0}", DateTime.Now);
            EnterPostContent(postContent);
            InsertPictureIntoPost_FromLocalhost(imageName);
            Publish();
            return postTitle + "&" + imageName;
        }


        public void RemoveBlogAccount(string blogName)
        {
            ClickBlogAccountDropdown();
            ManageBlogAccountFromToolBar();
            SelectBlogAccountToRemove(blogName);
            try { ClickRemove(); }
            catch(Exception)
            {}
            ConfirmRemoveBlogAccount();
            ClickOKAtOptionsPage();
        }

        public bool IsOkConnectBlog()
        {
            if (PopupWarningInfo()) return false;
            else return true;
        }

        public bool IsOkPublishPost()
        {
            if (PopupErrorInfoWindow()) return false;
            else return true;
        }

        public bool IsOkGetRecentPost()
        {
            return IsRecentPostAvailable();
        }

        public bool IsOkDeleteRecentPost()
        {
            if (PopupErrorInfoWindow()) return false;
            else return true;
        }

        public void DeleteTestTempPost(string titleAndSlugOrContentOrOther)
        {
            string postTitle = titleAndSlugOrContentOrOther.Split('&')[0];
            DeleteTestTempPostByTitle(postTitle);
        }
        
        public void DeleteTestTempPostByTitle(string title)
        {
            string pathPost = GetPostLocalPath(title);
            if (File.Exists(pathPost))
                File.Delete(pathPost);
        }

        public void DeleteTestTempPostDraft(string title)
        {
            string pathDraft = GetPostDraftLocalPath(title);
            if (File.Exists(pathDraft))
                File.Delete(pathDraft);
        }

        public string GetPostLocalPath(string title)
        {
            string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            return string.Format(@"{0}\My Weblog Posts\Recent Posts\{1}.wpost", myDocumentsPath, title);
        }

        public string GetPostDraftLocalPath(string title)
        {
            string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            return string.Format(@"{0}\My Weblog Posts\Drafts\{1}.wpost", myDocumentsPath, title);            
        }

        public void TryGetRecentPost(string postTitle)
        {
            TryToGetPostLinkDefautTestBlog(postTitle);
        }

        public bool TryOpenRecentPostFromRecentPostsList()
        {
           return TryOpenGotPostLinkFromPostsList();        
        }

        public void CloseSignInErrorWindowAndCancelSignIn()
        {
            CloseErrorWindow_SignIn();
            ClickCancelSignIn();
        }

        public void CloseSignInErrorWindowAndOkSignIn()
        {
            CloseErrorWindow_SignIn();
            ClickOkSignIn();
        }


        #region private function

        private string infoCheckModuleEnable = "Please check whether the Module Feature of 'Remote Blog Publishing' in Website is enable or not.";

        private void SignIn(string userName,string password)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxEmail';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(userName);
            element = ScreenElement.FromPartialQueryId(@";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxPassword';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(password);
            element = ScreenElement.FromPartialQueryId(@";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxSavePassword';[VisibleOnly]Role = 'check box'");
            element.Check();
            element = ScreenElement.FromPartialQueryId(@";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonOK';[VisibleOnly]Role = 'push button'");
            element.LeftButtonClick(-1, -1); 
        }

        public void Publish()
        {
            ScreenElement element;
            if (!RPF.IsAvailable(6000, @";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Publish' && Role = 'tool bar';[VisibleOnly]Name = 'Publish' && Role = 'push button'"))
            {
                element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Role = 'client' && ClassName = 'NetUIHWND';[VisibleOnly]Name = 'Home' && Role = 'page tab'");
                element.LeftButtonClick(10, 10);
            }
            element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Publish' && Role = 'tool bar';[VisibleOnly]Name = 'Publish' && Role = 'push button'");
            Thread.Sleep(1000);
            element.LeftButtonClick(-1, -1);
            Thread.Sleep(2000);
            if (RPF.IsAvailable(6000, @";Name => 'Windows Live Writer' && ClassName => '#32770';ControlId = '65535';[VisibleOnly]Role = 'text'"))
                throw (new Exception(infoCheckModuleEnable));
        }

        private void SetPostDate_Add1Minutes(string title_Writer)
        {
            //function: DateTime.Now.AddMinutes(1).ToString();
            int newHour = 0;
            int newMinutes = 0;
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => '" + title_Writer + " - Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'datePublishDate';[VisibleOnly]Role = 'drop down'");
            element.LeftButtonClick(10, 10);

            RPF.SendKeys(@"{RIGHT}{RIGHT}{RIGHT}{RIGHT}{RIGHT}"); //Press arrow right move focus onto "Minutes" postion
            if (DateTime.Now.Minute == 59)
            {
                RPF.SendKeys(@"{LEFT}");
                newHour = DateTime.Now.Hour + 1;
                if (newHour < 10)
                    RPF.SendKeys(@"{" + newHour + "}");
                else
                    RPF.SendKeys(@"{" + newHour.ToString()[0] + "}" + "{" + newHour.ToString()[1] + "}");
                //RPF.SendKeys(@"{RIGHT}");
                //RPF.SendKeys(@"{0}{1}");
            }
            else
            {                
                newMinutes = DateTime.Now.Minute + 1;
                if (newMinutes < 10)
                    element.SendKeys(@"{" + newMinutes + "}");
                else
                    element.SendKeys(@"{" + newMinutes.ToString()[0] + "}" + "{" + newMinutes.ToString()[1] + "}");
                
            }
        }

        public void SetPostDate_Add1Minutes()
        {
            //function: DateTime.Now.AddMinutes(1).ToString();
            int newHour = 0;
            int newMinutes = 0;
            ScreenElement element = ScreenElement.FromPartialQueryId(@"; ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'datePublishDate';[VisibleOnly]Role = 'drop down'");
            element.LeftButtonClick(10, 10);

            RPF.SendKeys(@"{RIGHT}{RIGHT}{RIGHT}{RIGHT}{RIGHT}"); //Press arrow right move focus onto "Minutes" postion
            if (DateTime.Now.Minute == 59)
            {
                RPF.SendKeys(@"{LEFT}");
                newHour = DateTime.Now.Hour + 1;
                if (newHour < 10)
                    RPF.SendKeys(@"{" + newHour + "}");
                else
                    RPF.SendKeys(@"{" + newHour.ToString()[0] + "}" + "{" + newHour.ToString()[1] + "}");
                //RPF.SendKeys(@"{RIGHT}");
                //RPF.SendKeys(@"{0}{1}");
            }
            else
            {
                newMinutes = DateTime.Now.Minute + 1;
                if (newMinutes < 10)
                    element.SendKeys(@"{" + newMinutes + "}");
                else
                    element.SendKeys(@"{" + newMinutes.ToString()[0] + "}" + "{" + newMinutes.ToString()[1] + "}");

            }
        }

        public void EnterPostTitle(string title)
        {
            //ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Untitled - Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'pane' && ClassName = 'Internet Explorer_Server';[VisibleOnly]Name = 'Enter a post title' && Role = 'editable text'");
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'pane' && ClassName = 'Internet Explorer_Server';[VisibleOnly]Name = 'Enter a post title' && Role = 'editable text'");
            element.LeftButtonClick(-1, -1);
            RPF.SendKeys(ConvertStringToKeys(title));  //RPF.SendKeys(@"{T}{H}"); RPF.SendKeys(@"{T},{H}"); //result  THT,H
            RPF.SendKeys(@"{ENTER}");
        }
                
        private void EditPostTitle(string origTitle,string NewTitle)
        {            
            //ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => '" + origTitle + " - Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'pane' && ClassName = 'Internet Explorer_Server';[VisibleOnly]Name = '" + origTitle + "' && Role = 'editable text'");
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => '" + origTitle + " - Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'pane';[VisibleOnly]Name = '" + origTitle + "' && Role = 'editable text'");
            //ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'pane';[VisibleOnly]Role = 'editable text'");
            // int length=element.Value.Length;
            element.LeftButtonClick(-1, -1);           
            int loop = origTitle.Length / 2 + 5;
            for (int i = 0; i < loop; i++)
            {
                try
                {
                    RPF.SendKeys(@"{DELETE}");
                    RPF.SendKeys(@"{BACKSPACE}");
                }
                catch (Exception)
                { }               
                
            }
            //string ss = element.Name;
            //element = ScreenElement.FromPartialQueryId(@";Name => '" + origTitle + " - Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'pane';[VisibleOnly]Name = '" + origTitle + "' && Role = 'editable text'");
           
            RPF.SendKeys(ConvertStringToKeys(NewTitle));  //RPF.SendKeys(@"{T}{H}"); RPF.SendKeys(@"{T},{H}"); //result  THT,H
            RPF.SendKeys(@"{ENTER}");
        }

        public void EnterPostContent(string content)
        {
            //if (string.IsNullOrEmpty(title_Writer))
            //    title_Writer = "Untitled";
            ////ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => '" + title_Writer + @" - Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Name = 'file://C:\Users\v-mew\AppData\Local\Temp\WindowsLiveWriter-1504035094\D68C78D59466\index.htm' && Role = 'client';[VisibleOnly]Role = 'pane' && ClassName = 'Internet Explorer_Server'");
            RPF.SendKeys(ConvertStringToKeys(content));
        }

        public string ConvertStringToKeys(string strings)
        {
            StringBuilder strB_title = new StringBuilder();
            //strB_title.Append('@');
            //strB_title.Append('"');
            foreach (char c in strings)
            {
                if (c == ' ')
                    strB_title.Append(' ');
                else
                {
                    strB_title.Append('{');
                    strB_title.Append(c);
                    strB_title.Append('}');
                }
            }
            //strB_title.Append('"');           
            return strB_title.ToString();
        }

        private void SigninWhenNeed()
        {
            if (RPF.IsAvailable(10000, @";Name = 'Sign in - -' && Role = 'window'"))
            {
                SignIn(TestLibrary.Consts.AdminUserName, TestLibrary.Consts.CommonPassword);
            }
        }

        private void SigninWhenNeed(string userName,string password)
        {           
            if (RPF.IsAvailable(10000, @";Name = 'Sign in - -' && Role = 'window'"))
            {
                SignIn(userName, password);
            }           
        }

        private void Save()
        {
              ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Application menu' && Role = 'grid drop down button'");
              element.LeftButtonClick(1, 1);
              element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Save' && Role = 'grouping';[VisibleOnly]Name = 'Save' && Role = 'split button'");
              element.LeftButtonClick(10, 10);           
        }

        private void SpecifySlug(string postSlug)
        {
            ViewAll_Click();
            EnterSlugForPost(postSlug);
        }

        private void ViewAll_Click()
        {
            //if (string.IsNullOrEmpty(postTitle))
            //    postTitle = "Untitled";
            //ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => '"+postTitle+" - Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'linkViewAll';[VisibleOnly]Role = 'link'");
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'linkViewAll';[VisibleOnly]Role = 'link'");
            element.LeftButtonClick(1, 1);
        }

        private void EnterSlugForPost(string slug)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Post Properties' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textSlug';[VisibleOnly]Role = 'editable text'");
            element.LeftButtonClick(1, 1);
            element.SendKeys(ConvertStringToKeys(slug));
            element = ScreenElement.FromPartialQueryId(@";Name => 'Post Properties' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonClose';[VisibleOnly]Role = 'push button'");
            element.LeftButtonClick(1,1);
            //RPF.SendKeys(@"{ENTER}");
        }

        public void EnterPostTag(string tag)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textTags';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(tag);
        }

        private void EditPostTag(string newTag)
        {
            EnterPostTag(newTag);
        }

        private void OpenUpdateAccountForm()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Role = 'client' && ClassName = 'NetUIHWND';[VisibleOnly]Name = 'Blog Account' && Role = 'page tab'");
            element.LeftButtonClick(1, 1);
            element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Blog options' && Role = 'push button'");
            element.LeftButtonClick(1, 1);
            element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonEditConfiguration';[VisibleOnly]Role = 'push button'");
            element.LeftButtonClick(1, 1);
        }

        private void EnterBlogAccountInfo(string blogHomePageUrl,string userName,string password)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxHomepageUrl';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(blogHomePageUrl);    
            element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxUsername';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(userName);
            element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxPassword';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(password);           
            Thread.Sleep(3000);
        }

        private void DownLoadThemes(bool downLoadThemes)
        {
            ScreenElement element ;
            //if(RPF.IsAvailable(15000,@";Name => 'Download Blog Theme' && ClassName => '#32770';ControlId = '6';[VisibleOnly]Role = 'push button'"))
            while(!RPF.IsAvailable(1000, @";Name => 'Download Blog Theme' && ClassName => '#32770';ControlId = '6';[VisibleOnly]Role = 'push button'"))
            Thread.Sleep(1000);
            {
                if(downLoadThemes)
                {
                    element = ScreenElement.FromPartialQueryId(@";Name => 'Download Blog Theme' && ClassName => '#32770';ControlId = '6';[VisibleOnly]Role = 'push button'");
                    element.LeftButtonClick(1,1);
                    Thread.Sleep(1000);
                }
                else
                {
                    element = ScreenElement.FromPartialQueryId(@";Name => 'Download Blog Theme' && ClassName => '#32770';ControlId = '7';[VisibleOnly]Role = 'push button'");
                    element.LeftButtonClick(1,1);
                }
            }
            
        }


        //private void EnterBlogNameWhenNeed(string blogName)
        //{
        //    ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxWeblogName';[VisibleOnly]Role = 'editable text'");
        //    if (string.IsNullOrEmpty(element.Value))
        //        element.SetValueAsEditBox(blogName);            
        //}

        private void EnterBlogName(string blogName)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxWeblogName';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(blogName);
        }

        private void CheckRemeberPassword(bool ifCheck)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxSavePassword';[VisibleOnly]Role = 'check box'");
            if (ifCheck) element.Check();
            else element.Uncheck();
        }              

        private void ClickFinish()
        {
            ClickNext();
        }

        private void ClickOKAtUpdateAccount()
        {
            string queryID= @";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonOK';[VisibleOnly]Role = 'push button'";
            for (int i = 1; i <= 3; i++)
            {
                if (RPF.IsAvailable(3000, queryID))
                {
                    ScreenElement element = ScreenElement.FromPartialQueryId(queryID);
                    element.LeftButtonClick(1, 1);
                    break;
                }
            }
        }

        private void OpenMenuOptionPanel()
        {
            OpenMenu();
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Ribbon' && ClassName => 'UIRibbonWorkPane';[VisibleOnly]Name= 'Options' && Role = 'menu item'");
            element.LeftButtonClick(10, 10);
            //element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly, VisibleOnly]ControlName = 'btnPostEditorPreferencesPanel';[VisibleOnly]Role = 'push button'");  //defaut option tab
        }

        private void EnterPublishingSettings(bool isCheckViewBlog,bool isCheckCloseWindow,bool isCheckReminderTile,bool isCheckReminderCategory,bool isCheckReminderTag)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxViewWeblog';[VisibleOnly]Role = 'check box'");
            if (isCheckViewBlog) element.Check(); else element.Uncheck();
            element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxCloseWindow';[VisibleOnly]Role = 'check box'");
            if (isCheckCloseWindow) element.Check(); else element.Check();
            element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'selectCloseOptions';[VisibleOnly]Role = 'combo box'");
            if (element.Value != "Always")
                element.SendKeys(@"{UP}");
            element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxTitleReminder';[VisibleOnly]Role = 'check box'");
            if (isCheckReminderTile) element.Check(); else element.Uncheck();
            element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxCategoryReminder';[VisibleOnly]Role = 'check box'");
            if (isCheckReminderCategory) element.Check(); else element.Uncheck();
            element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxTagReminder';[VisibleOnly]Role = 'check box'");
            if (isCheckReminderTag) element.Check(); else element.Uncheck();            
        }

        private void ClickOKAtOptionsPage()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonOK';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void OpenMenu()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Application menu' && Role = 'grid drop down button'");
            element.LeftButtonClick(10, 10);            
        }

        private void OpenMenuRecentPostLinkBlog()
        {
            OpenMenu();
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Open recent post' && Role = 'grouping';[VisibleOnly]Name = 'Open recent post' && Role = 'split button'");
            element.LeftButtonClick(10, 10);
            try
            {
                //selenium.KeyPressNative("40");  //down arrow ascii : 40
                Thread.Sleep(2000);
                RPF.SendKeys(@"{RIGHT}");
                Thread.Sleep(4000);
                RPF.SendKeys(@"{ }");               
                //RPF.SendKeys(@"{Tab}");
            }
            catch (Exception)
            {
            }
        }
       
        private bool IsRecentPostAvailable()
        {
            OpenMenu();
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Open recent post' && Role = 'grouping';[VisibleOnly]Name = 'Open recent post' && Role = 'split button'");
            element.LeftButtonClick(10, 10);
            try
            {
                Thread.Sleep(2000);
                RPF.SendKeys(@"{RIGHT}");
                Thread.Sleep(4000);
                RPF.SendKeys(@"{ }");  
            }
            catch (Exception)
            {
            }
            if (RPF.IsAvailable(6000, @";Name = 'Server Error' && Role = 'window'"))
                return false;
            else return true;
        }

        private void OpenPostLinkDefautTestBlog(string postTitle)
        {
            Thread.Sleep(12000);
            OpenMenuRecentPostLinkBlog();

            FilterPost(postTitle);
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Open' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'listBoxPosts';[VisibleOnly]Role = 'list'");
            try { element.DoubleClick(10, 10); }
            catch (Exception)
            { }
        }

        private void TryToGetPostLinkDefautTestBlog(string postTitle)
        {
            Thread.Sleep(12000);
            OpenMenuRecentPostLinkBlog();
            FilterPost(postTitle);
        }

        private bool TryOpenGotPostLinkFromPostsList()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Open' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'listBoxPosts';[VisibleOnly]Role = 'list'");
            try { element.DoubleClick(10, 10); }
            catch (Exception)
            { }
            if (PopupErrorInfoWindow()) return false;
            else return true;
        }

        private void OpenRecentPostByTitleFromDefautTestBlog(string postTitle)
        {
            Thread.Sleep(9000);
            OpenMenuRecentPostLinkBlog();
            FilterPost(postTitle);                     
        }

        private void PostDraftToBlog()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Publish' && Role = 'tool bar';[VisibleOnly]Name = 'Post draft to blog' && Role = 'push button'");
            element.LeftButtonClick(10, 10);
        }

        public void InsertPictureIntoPost_FromWeb(string pictureUrl)
        {
            Thread.Sleep(1000);
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Role = 'client' && ClassName = 'NetUIHWND';[VisibleOnly]Name = 'Insert' && Role = 'page tab'");
            element.LeftButtonClick(5,5);
            element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Media' && Role = 'tool bar';[VisibleOnly]Name = 'Picture' && Role = 'drop down button'");
            element.LeftButtonClick(10, 10);
            element = ScreenElement.FromPartialQueryId(@";Name => 'Ribbon' && ClassName => 'UIRibbonWorkPane';[VisibleOnly]Name = 'From the web...' && Role = 'menu item'");
            element.LeftButtonClick(5, 5);
            Thread.Sleep(3000);
            element = ScreenElement.FromPartialQueryId(@";Name => 'Insert Web Image' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'imageUrl';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(pictureUrl);
            element = ScreenElement.FromPartialQueryId(@";Name => 'Insert Web Image' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonInsert';[VisibleOnly]Role = 'push button'");
            element.LeftButtonClick(5, 5);
            Thread.Sleep(3000);
        }

        public void InsertPictureIntoPost_FromLocalhost(string imageName)
        {
            Thread.Sleep(1000);
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Role = 'client' && ClassName = 'NetUIHWND';[VisibleOnly]Name = 'Insert' && Role = 'page tab'");
            element.LeftButtonClick(5, 5);
            element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Media' && Role = 'tool bar';[VisibleOnly]Name = 'Picture' && Role = 'drop down button'");
            element.LeftButtonClick(10, 10);
            element = ScreenElement.FromPartialQueryId(@";Name => 'Ribbon' && ClassName => 'UIRibbonWorkPane';[VisibleOnly]Name = 'From your computer...' && Role = 'menu item'");
            element.LeftButtonClick(5, 5);
            Thread.Sleep(3000);
            element = ScreenElement.FromPartialQueryId(@";Name => 'Insert Picture' && ClassName => '#32770';ControlId = '1148';[VisibleOnly]Role = 'editable text'");
            element.SetValueAsEditBox(string.Format(@"C:\Users\Public\Pictures\Sample Pictures\{0}",imageName));
            //element = ScreenElement.FromPartialQueryId(@";Name => 'Insert Picture' && ClassName => '#32770';ControlId = '1';[VisibleOnly]Role = 'split button'");
            try
            {
                RPF.SendKeys(@"{TAB}"); Thread.Sleep(1);
                RPF.SendKeys(@"{TAB}"); Thread.Sleep(1);
                RPF.SendKeys(@"{ENTER}");
            }
            catch (Exception)
            { }
            Thread.Sleep(3000);
        }

        private void ClickNext()
        {
            Thread.Sleep(2000);                                                                
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonNext';[VisibleOnly]Role = 'push button'");
            element.LeftButtonClick(10, 10);
        }

        private void SwitchToBlog(bool ifCheck)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Windows Live Writer' && ClassName =>'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'checkBoxSwitchToWeblog';[VisibleOnly]Role = 'check box'");
            if (ifCheck) element.Check();
            else element.Uncheck();
        }

        private void SelectOtherType_AddBlog()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Windows Live Writer' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'radioButtonOther';[VisibleOnly]Role = 'radio button'");
            element.LeftButtonClick(5, 5);
            //element.Check();
        }

        private void ClickBlogAccountDropdown()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'UIRibbonDockTop' && ClassName => 'UIRibbonCommandBarDock';[VisibleOnly]Name = 'Publish' && Role = 'tool bar';[VisibleOnly]Role = 'drop down button'");
            element.LeftButtonClick(10, 10);           
        }

        private void OpenAddBlogAccountDialogFromToolBar()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'NetUIHWND';[VisibleOnly]Name = 'Add blog account...' && Role = 'menu item'");
            try
            {
                element.LeftButtonClick(10, 10);
            }
            catch (Exception)
            { }            
        }

        private void CheckXmlRpcModuleStatusOfWeb()
        {
            if (RPF.IsAvailable(9000, @";Name => 'Update Account Configuration' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'comboBoxSelectProvider';[VisibleOnly]Role = 'combo box'"))
                throw (new Exception(infoCheckModuleEnable));  //if XmlRpc module is disable in Website, throw error info.
        }

        private bool CheckBlogExist(string blogName)
        {
            if(RPF.IsAvailable(6000,@";ClassName => 'NetUIHWND';[VisibleOnly]Name = '"+blogName+"' && Role = 'menu item'"))
                return true;
            else return false;
        }

        private void SelectBlog(string blogName)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'NetUIHWND';[VisibleOnly]Name = '" + blogName + "' && Role = 'menu item'");
            element.LeftButtonClick(10, 10);
        }

        private void ManageBlogAccountFromToolBar()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'NetUIHWND';[VisibleOnly]Name = 'Manage blog accounts...' && Role = 'menu item'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void SelectBlogAccountToRemove(string blogName)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'list' && ClassName => 'WindowsForms10.SysListView32.app';Name = ' " + blogName + "' && Role = 'list item'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void ClickRemove()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Options' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonDelete';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void ConfirmRemoveBlogAccount()
        {                                                                  
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Windows Live Writer' && ClassName => '#32770';ControlId = '6';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void ClickDelete()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Open' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonDelete';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void ConfirmDelete(bool delete)
        {
            ScreenElement element;
            if (delete)
            {
                element = ScreenElement.FromPartialQueryId(@";Name => 'Confirm Delete' && ClassName => '#32770';ControlId = '6';[VisibleOnly]Role = 'push button'");
                try { element.LeftButtonClick(10, 10); }
                catch (Exception)
                { }
            }
            else
            {
                element = ScreenElement.FromPartialQueryId(@";Name => 'Confirm Delete' && ClassName => '#32770';ControlId = '7';[VisibleOnly]Role = 'push button'");
                try { element.LeftButtonClick(10, 10); }
                catch (Exception)
                { }
            }
        }

        private void CloseOpenedWindow()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Open' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'title bar' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Name = 'Close' && Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void CloseLiveWriterWindowAndClickCancelSaveChangesIfPopup()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'title bar' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Name = 'Close' && Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
            if (RPF.IsAvailable(6000, @";Name => 'Save Changes' && ClassName => '#32770';ControlId = '7';[VisibleOnly]Role = 'push button'"))
            {
                element = ScreenElement.FromPartialQueryId(@";Name => 'Save Changes' && ClassName => '#32770';ControlId = '7';[VisibleOnly]Role = 'push button'");
                try { element.LeftButtonClick(10, 10); }
                catch (Exception)
                { }
            }
        }

        private void FilterPost(string postTitle)
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Open' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'textBoxFilter';[VisibleOnly]Role = 'editable text'");
            try { element.SetValueAsEditBox(postTitle); }
            catch (Exception)
            { }  
        }

        private bool PopupWarningInfo()
        {
            if(RPF.IsAvailable(15000,@";Name => 'Windows Live Writer' && ClassName => '#32770';ControlId = '65535';[VisibleOnly]Role = 'text'"))
                return true;
            else  return false;
        }

        private bool PopupErrorInfoWindow()
        {
            if(RPF.IsAvailable(6000,@";Name = 'Server Error' && Role = 'window'"))
                return true;
            else if(RPF.IsAvailable(6000,@";Name => 'Server Error' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Name = 'Server Error' && Role = 'client'"))
                return true;
            else if(RPF.IsAvailable(6000,@";Name = 'Network Connection Error' && Role = 'window'"))
                return true;
            else return false;
        }

        private void TryAddBlogByInvalidAccount(string blogHomePageUrl, string blogName, string userName, string password)
        {
            Thread.Sleep(3000);
            OpenAddBlogAccountDialogFromToolBar();
            Thread.Sleep(2000);
            SelectOtherType_AddBlog();
            ClickNext();
            EnterBlogAccountInfo(blogHomePageUrl, userName, password);
            ClickNext();
        }

        private void CloseErrorWindow_NetworkConnect()
        {
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Network Connection Error' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonOK';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        //private void CloseLiveWrterWindowAtEndIfExist()
        //{
        //    ScreenElement element;
        //    try
        //    {
        //        if (RPF.IsAvailable(3000, @";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'title bar' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Name = 'Close' && Role = 'push button'"))
        //        {
        //            element = ScreenElement.FromPartialQueryId(@";ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Role = 'title bar' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]Name = 'Close' && Role = 'push button'");
        //            element.LeftButtonClick(5, 5);
        //        }
        //    }
        //    catch (Exception)
        //    { }

        //}

        private void CloseErrorWindow_SignIn()
        {
            if (!RPF.IsAvailable(6000, @";Name => 'Windows Live Writer' && ClassName => '#32770';ControlId = '2';[VisibleOnly]Role = 'push button'"))
                return;
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Windows Live Writer' && ClassName => '#32770';ControlId = '2';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void ClickCancelSignIn()
        {
            if (!RPF.IsAvailable(6000, @";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonCancel';[VisibleOnly]Role = 'push button'"))
                return;
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonCancel';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void ClickOkSignIn()
        {
            if (!RPF.IsAvailable(6000, @";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonOk';[VisibleOnly]Role = 'push button'"))
                return;
            ScreenElement element = ScreenElement.FromPartialQueryId(@";Name => 'Sign in - -' && ClassName => 'WindowsForms10.Window.8.app';[VisibleOnly]ControlName = 'buttonOk';[VisibleOnly]Role = 'push button'");
            try { element.LeftButtonClick(10, 10); }
            catch (Exception)
            { }
        }

        private void CloseRecoverUnsavedChangedWindow()
        {
            string discardButtonQueryId=@";Name => 'Windows Live Writer' && ClassName => '#32770';[VisibleOnly]Name = 'Windows Live Writer' && Role = 'pane';[VisibleOnly]Name = 'Discard changes permanently' && Role = 'push button'";
            string confirmCloseQueryId=@";Name => 'Discard Changes' && ClassName => '#32770';ControlId = '6';[VisibleOnly]Role = 'push button'";
            if (RPF.IsAvailable(5000,discardButtonQueryId))   //deal with "Recover unsaved changes" prompt window because Live Writer didn't shut down properly last time.
            {
                ScreenElement element = ScreenElement.FromPartialQueryId(discardButtonQueryId);
                try { element.LeftButtonClick(10, 10); }
                catch (Exception)
                { }
            }
            if (RPF.IsAvailable(5000, confirmCloseQueryId))
            {
                ScreenElement element = ScreenElement.FromPartialQueryId(confirmCloseQueryId);
                try { element.LeftButtonClick(10, 10); }
                catch (Exception)
                { }
            }
        }

        #endregion

    }
}
